package com.lrkj.property.service.impl;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.Community;
import com.lrkj.property.domain.SiteManag;
import com.lrkj.property.mapper.SiteManagMapper;
import com.lrkj.property.service.ICommunityService;
import com.lrkj.property.service.ISiteManagService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 场地管理Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-16
 */
@Service
public class SiteManagServiceImpl implements ISiteManagService {
    @Autowired
    private SiteManagMapper siteManagMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private ICommunityService communityService;

    /**
     * 查询场地管理
     *
     * @param id 场地管理ID
     * @return 场地管理
     */
    @Override
    public SiteManag selectSiteManagById(String id) {
        return siteManagMapper.selectSiteManagById(id);
    }

    /**
     * 查询场地管理列表
     *
     * @param siteManag 场地管理
     * @return 场地管理
     */
    @Override
    public List<SiteManag> selectSiteManagList(SiteManag siteManag) {
        return siteManagMapper.selectSiteManagList(siteManag);
    }

    /**
     * 新增场地管理
     *
     * @param siteManag 场地管理
     * @return 结果
     */
    @Override
    public int insertSiteManag(SiteManag siteManag) {
        siteManag.setId(IdUtils.simpleUUID());
        siteManag.setCreateBy(SecurityUtils.getUsername());
        siteManag.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(siteManag);
        return siteManagMapper.insertSiteManag(siteManag);
    }

    /**
     * 修改场地管理
     *
     * @param siteManag 场地管理
     * @return 结果
     */
    @Override
    public int updateSiteManag(SiteManag siteManag) {
        siteManag.setUpdateTime(DateUtils.getNowDate());
        siteManag.setUpdateBy(SecurityUtils.getUsername());
        this.setUnitInfo(siteManag);
        return siteManagMapper.updateSiteManag(siteManag);
    }

    private void setUnitInfo(SiteManag bean) {
        Community parentBean = communityService.selectCommunityById(bean.getCommunityId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getCommunityId());
        bean.setDeptId(parentBean.getDeptId());
        orgTreeService.insertOrgTree(bean.getId(), bean.getSiteName(), bean.getCommunityId(), OrgConstants.site, parentBean.getAncestors(), bean.getOrderNum());
    }


    /**
     * 批量删除场地管理
     *
     * @param ids 需要删除的场地管理ID
     * @return 结果
     */
    @Override
    public int deleteSiteManagByIds(String[] ids) {
        orgTreeService.deleteOrgTreeByIds(ids);
        return siteManagMapper.deleteSiteManagByIds(ids);
    }

    /**
     * 删除场地管理信息
     *
     * @param id 场地管理ID
     * @return 结果
     */
    @Override
    public int deleteSiteManagById(String id) {
        return siteManagMapper.deleteSiteManagById(id);
    }

    @Override
    public Map<String, Object> getSitePostInfo(String siteId) {
        return siteManagMapper.getSitePostInfo(siteId);
    }

    /**
     * 批量新增场地管理信息
     *
     * @param siteManags 场地管理对象
     */
    public int batchAddData(List<SiteManag> siteManags) {
        return siteManagMapper.batchAddData(siteManags);
    }
}
